package com.dtranx.tools.commons.vo;

import java.io.Serializable;

public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = -3310880601126072343L;
    private boolean success;
    private String msg;
    private Integer code;
    private String msgInEnglish;
    private T data;
    private String zhMsg;
    private Object[] args;

    public static AbstractLocal abstractLocal;

    public ResponseResult(){}

    public ResponseResult(boolean success, String msg, T data, Integer code, Object... args) {
        this.success = success;
        initMsg(msg, code, args);
        this.zhMsg = msg;
        this.data = data;
    }

    public ResponseResult(boolean success, String msg, String msgInEnglish, T data, Integer code, Object... args) {
        this.success = success;
        initMsg(msg,code,args);
        this.msgInEnglish = msgInEnglish;
        this.zhMsg = msg;
        this.data = data;
    }

    private void initMsg(String msg,Integer code,Object... args){
        if (abstractLocal != null && code != null) {
            this.msg = abstractLocal.getMessage(code,args) == null ? msg : abstractLocal.getMessage(code,args);
        } else {
            this.msg = msg;
        }
        this.code = code;
        this.args = args;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsgInEnglish() {
        return msgInEnglish;
    }

    public String getZhMsg() {
        return zhMsg;
    }

    public void setZhMsg(String zhMsg) {
        this.zhMsg = zhMsg;
    }

    public void setMsgInEnglish(String msgInEnglish) {
        this.msgInEnglish = msgInEnglish;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("ResponseResult{");
        sb.append("success=").append(success);
        sb.append(", msg='").append(msg).append('\'');
        sb.append(", msgInEnglish='").append(msgInEnglish).append('\'');
        sb.append(", data=").append(data);
        sb.append('}');
        return sb.toString();
    }

    /**
     * 返回为空，成功返回
     * @return
     */
    public static ResponseResult success(){
        return new ResponseResult(true,"操作成功","Operation Succeeded!",100);
    }

    /**
     * 带数据返回成功
     * @param data
     * @return
     */
    public static ResponseResult success(Object data){
        return new ResponseResult(true,"操作成功","Operation Succeeded!",data,100);
    }

    /**
     *
     * @param data
     * @param msg
     * @return 返回数据+自定义信息 成功状态封装类
     */
    public static ResponseResult success(Object data, String msg,Integer code,Object... args) {
        return new ResponseResult(true, msg, null, data, code, args);
    }

    /**
     *
     * @param data
     * @param msg
     * @return 返回数据+自定义信息 成功状态封装类
     */
    public static ResponseResult success(Object data, String msg) {
        return new ResponseResult(true,msg,null,data,null);
    }


    /**
     *
     * @param msg
     * @param msgInEnglish
     * @return 自定义信息 成功状态封装类
     */
    public static ResponseResult success(String msg, String msgInEnglish,Integer code,Object... args){
        return new ResponseResult(true,msg,msgInEnglish,code,args);
    }

    /**
     *
     * @param data
     * @param msg
     * @param msgInEnglish
     * @return 返回数据+自定义信息 成功状态封装类
     */
    public static ResponseResult success(Object data, String msg, String msgInEnglish,Integer code,Object... args){
        return new ResponseResult(true,msg,msgInEnglish,data,code,args);
    }

    /**
     * 带失败信息返回失败
     * @param msg
     * @return
     */
    public static ResponseResult failure(String msg,Integer code,Object... args) {
        return new ResponseResult(false,msg,null,code,args);
    }

    /**
     * 带失败信息返回失败
     *
     * @param msg
     * @param msgInEnglish
     * @return
     */
    public static ResponseResult failure(String msg, String msgInEnglish,Integer code, Object... args) {
        return new ResponseResult(false,msg,msgInEnglish,code,args);
    }

    /**
     * 带失败信息及数据
     * @param msg
     * @param msgInEnglish
     * @param data 当次操作数据结果
     * @return
     */
    public static ResponseResult failure(String msg, String msgInEnglish, Object data,Integer code,Object... args) {
        return new ResponseResult(false,msg,msgInEnglish,data,code,args);
    }
}
